package com.interview.ujiuye10;

import java.util.concurrent.*;

/**
 * Copyright (C), 2018-2020
 * FileName: Demo5
 * Author:   kongfanyu
 * Date:     2020/12/24 下午7:54
 */
public class Demo5 {
    //创建阻塞队列，存储的类型是线程
    private static BlockingQueue<Thread> bq = new ArrayBlockingQueue<>(3);
    public static void main(String[] args) throws InterruptedException {
        Runnable r =  () -> {
            System.out.println(Thread.currentThread().getName() + "-----执行-----");
        };
        //向队列添加线程
        bq.add(new Thread(r,"线程A"));
        bq.add(new Thread(r,"线程B"));
        bq.add(new Thread(r,"线程C"));

        //循环10轮
        for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 3; j++) {
                //从阻塞队列取任务
                Thread thread = bq.take();
                thread.start();
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
            }
            //再添加三个线程
            bq.add(new Thread(r,"线程A"));
            bq.add(new Thread(r,"线程B"));
            bq.add(new Thread(r,"线程C"));
        }

        ExecutorService pool = Executors.newFixedThreadPool(3);

    }

}
